package com.chaodev.base.controller;

import com.chaodev.base.base.RedisDao;
import com.chaodev.base.dto.ExplanationPointDto;
import com.chaodev.base.model.ExplanationPoint;
import com.chaodev.base.model.Result;
import com.chaodev.base.model.ResultEnum;
import com.chaodev.base.model.UserInfo;
import com.chaodev.base.service.ExplanationPointService;
import com.chaodev.base.service.ILogService;
import com.chaodev.base.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("explanationPoint")
@Api(tags = "导览解说点")
public class ExplanationPointController {

    @Autowired
    private RedisDao redisDao;

    @Autowired
    private ILogService logService;

    @Autowired
    private ExplanationPointService explanationPointService;

    @PostMapping(value = "/addExplanationPoint")
    @ApiOperation(value = "新增导览解说点")
    @ApiImplicitParam(name = "token", paramType = "header", value = "token", dataType = "string", required = true)
    public Result addExplanationPoint(@RequestBody ExplanationPoint explanationPoint, HttpServletRequest request, HttpServletResponse response){
        Result result = new Result();
        String token = request.getHeader("token");
        Result logResult = new Result();
        UserInfo userInfo = new UserInfo();
        try {
            if (!redisDao.existsKey(token)) {
                result.setCode(ResultEnum.EREOR.getCode());
                result.setMsg("登录超时");
                return result;
            }

            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);
            explanationPoint.setCreateUser(userInfo.getUserId());
            if (explanationPointService.addExplanationPoint(explanationPoint)){
                result.setCode(ResultEnum.SUCCESS.getCode());
                result.setMsg("操作成功");
            }else {
                result.setCode(ResultEnum.DB_UPDATE_FAILURE.getCode());
                result.setMsg("操作失败");
            }
        } catch (Exception exception) {
            result.setCode(ResultEnum.UNKNOWNERROR.getCode());
            result.setMsg("发生错误:" + exception.getMessage());
        } finally {
            BeanUtils.copyProperties(result, logResult);
            logResult.setData(null);
            String resultCode = logResult.getCode().equals(ResultEnum.SUCCESS.getCode()) ? "SUCCESS" : "ERROR";
            logService.logAdd(explanationPoint.toString(), "新增导览解说点", resultCode, logResult.toString(), userInfo, request);
        }
        return result;
    }

    @PostMapping(value = "/editExplanationPoint")
    @ApiOperation(value = "修改导览解说点")
    @ApiImplicitParam(name = "token", paramType = "header", value = "token", dataType = "string", required = true)
    public Result editExplanationPoint(@RequestBody ExplanationPoint explanationPoint, HttpServletRequest request, HttpServletResponse response){
        Result result = new Result();
        String token = request.getHeader("token");
        Result logResult = new Result();
        UserInfo userInfo = new UserInfo();
        try {
            if (!redisDao.existsKey(token)) {
                result.setCode(ResultEnum.EREOR.getCode());
                result.setMsg("登录超时");
                return result;
            }
            if (explanationPoint.getId() == null || "".equals(explanationPoint.getId())) {
                result.setCode(ResultEnum.DB_UPDATE_FAILURE.getCode());
                result.setMsg("缺少id");
                return result;
            }
            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);
            explanationPoint.setUpdateUser(userInfo.getUserId());
            if (explanationPointService.editExplanationPoint(explanationPoint)){
                result.setCode(ResultEnum.SUCCESS.getCode());
                result.setMsg("操作成功");
            }else {
                result.setCode(ResultEnum.DB_UPDATE_FAILURE.getCode());
                result.setMsg("操作失败");
            }
        } catch (Exception exception) {
            result.setCode(ResultEnum.UNKNOWNERROR.getCode());
            result.setMsg("发生错误:" + exception.getMessage());
        } finally {
            BeanUtils.copyProperties(result, logResult);
            logResult.setData(null);
            String resultCode = logResult.getCode().equals(ResultEnum.SUCCESS.getCode()) ? "SUCCESS" : "ERROR";
            logService.logAdd(explanationPoint.toString(), "修改导览解说点", resultCode, logResult.toString(), userInfo, request);
        }
        return result;
    }

    @PostMapping(value = "/saveExplanationPoint")
    @ApiOperation(value = "保存导览解说点")
    @ApiImplicitParam(name = "token", paramType = "header", value = "token", dataType = "string", required = true)
    public Result saveExplanationPoint(@RequestBody ExplanationPoint explanationPoint, HttpServletRequest request, HttpServletResponse response){
        Result result = new Result();
        String token = request.getHeader("token");
        Result logResult = new Result();
        UserInfo userInfo = new UserInfo();
        try {
            if (!redisDao.existsKey(token)) {
                result.setCode(ResultEnum.EREOR.getCode());
                result.setMsg("登录超时");
                return result;
            }
            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);
            explanationPoint.setCreateUser(userInfo.getUserId());
            if (explanationPointService.saveExplanationPoint(explanationPoint)){
                result.setCode(ResultEnum.SUCCESS.getCode());
                result.setMsg("操作成功");
            }else {
                result.setCode(ResultEnum.DB_UPDATE_FAILURE.getCode());
                result.setMsg("操作失败");
            }
        } catch (Exception exception) {
            result.setCode(ResultEnum.UNKNOWNERROR.getCode());
            result.setMsg("发生错误:" + exception.getMessage());
        } finally {
            BeanUtils.copyProperties(result, logResult);
            logResult.setData(null);
            String resultCode = logResult.getCode().equals(ResultEnum.SUCCESS.getCode()) ? "SUCCESS" : "ERROR";
            logService.logAdd(explanationPoint.toString(), "保存导览解说点", resultCode, logResult.toString(), userInfo, request);
        }
        return result;
    }

    @PostMapping(value = "/removeExplanationPoint")
    @ApiOperation(value = "删除导览解说点")
    @ApiImplicitParam(name = "token", paramType = "header", value = "token", dataType = "string", required = true)
    public Result removeExplanationPoint(@RequestBody ExplanationPoint explanationPoint, HttpServletRequest request, HttpServletResponse response){
        Result result = new Result();
        String token = request.getHeader("token");
        Result logResult = new Result();
        UserInfo userInfo = new UserInfo();
        try {
            if (!redisDao.existsKey(token)) {
                result.setCode(ResultEnum.EREOR.getCode());
                result.setMsg("登录超时");
                return result;
            }
            if (explanationPoint.getId() == null || "".equals(explanationPoint.getId())) {
                result.setCode(ResultEnum.DB_UPDATE_FAILURE.getCode());
                result.setMsg("缺少id");
                return result;
            }
            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);
            if (explanationPointService.removeExplanationPoint(explanationPoint)){
                result.setCode(ResultEnum.SUCCESS.getCode());
                result.setMsg("操作成功");
            }else {
                result.setCode(ResultEnum.DB_UPDATE_FAILURE.getCode());
                result.setMsg("操作失败");
            }
        } catch (Exception exception) {
            result.setCode(ResultEnum.UNKNOWNERROR.getCode());
            result.setMsg("发生错误:" + exception.getMessage());
        } finally {
            BeanUtils.copyProperties(result, logResult);
            logResult.setData(null);
            String resultCode = logResult.getCode().equals(ResultEnum.SUCCESS.getCode()) ? "SUCCESS" : "ERROR";
            logService.logAdd(explanationPoint.toString(), "删除导览解说点", resultCode, logResult.toString(), userInfo, request);
        }
        return result;
    }

    @PostMapping(value = "/getExplanationPointList")
    @ApiOperation(value = "查询导览解说点列表")
    @ApiImplicitParam(name = "token", paramType = "header", value = "token", dataType = "string", required = true)
    public Result getExplanationPointList(@RequestBody ExplanationPointDto explanationPointDto, HttpServletRequest request, HttpServletResponse response){
        Result result = new Result();
//        String token = request.getHeader("token");
        Result logResult = new Result();
        UserInfo userInfo = new UserInfo();
        try {
//            if (!redisDao.existsKey(token)) {
//                result.setCode(ResultEnum.EREOR.getCode());
//                result.setMsg("登录超时");
//                return result;
//            }
//            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);

            result.setData(explanationPointService.getExplanationPointList(explanationPointDto));
            result.setCode(ResultEnum.SUCCESS.getCode());
            result.setMsg("查询成功");
        } catch (Exception exception) {
            result.setCode(ResultEnum.UNKNOWNERROR.getCode());
            result.setMsg("发生错误:" + exception.getMessage());
        } finally {
            BeanUtils.copyProperties(result, logResult);
            logResult.setData(null);
            String resultCode = logResult.getCode().equals(ResultEnum.SUCCESS.getCode()) ? "SUCCESS" : "ERROR";
            logService.logAdd(explanationPointDto.toString(), "查询导览解说点列表", resultCode, logResult.toString(), userInfo, request);
        }
        return result;
    }

    @PostMapping(value = "/getExplanationPointById")
    @ApiOperation(value = "查询单条导览解说点")
    @ApiImplicitParam(name = "token", paramType = "header", value = "token", dataType = "string", required = true)
    public Result getExplanationPointById(@RequestBody ExplanationPoint explanationPoint, HttpServletRequest request, HttpServletResponse response){
        Result result = new Result();
//        String token = request.getHeader("token");
        Result logResult = new Result();
        UserInfo userInfo = new UserInfo();
        try {
//            if (!redisDao.existsKey(token)) {
//                result.setCode(ResultEnum.EREOR.getCode());
//                result.setMsg("登录超时");
//                return result;
//            }
            if (explanationPoint.getId() == null || "".equals(explanationPoint.getId())) {
                result.setCode(ResultEnum.DB_UPDATE_FAILURE.getCode());
                result.setMsg("缺少id");
                return result;
            }
//            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);

            result.setData(explanationPointService.getExplanationPointById(explanationPoint));
            result.setCode(ResultEnum.SUCCESS.getCode());
            result.setMsg("查询成功");
        } catch (Exception exception) {
            result.setCode(ResultEnum.UNKNOWNERROR.getCode());
            result.setMsg("发生错误:" + exception.getMessage());
        } finally {
            BeanUtils.copyProperties(result, logResult);
            logResult.setData(null);
            String resultCode = logResult.getCode().equals(ResultEnum.SUCCESS.getCode()) ? "SUCCESS" : "ERROR";
            logService.logAdd(explanationPoint.toString(), "查询单条导览解说点", resultCode, logResult.toString(), userInfo, request);
        }
        return result;
    }

    @PostMapping(value = "/getExplanationPointPage")
    @ApiOperation(value = "查询导览解说点分页")
    @ApiImplicitParam(name = "token", paramType = "header", value = "token", dataType = "string", required = true)
    public Result getExplanationPointPage(@RequestBody ExplanationPointDto explanationPointDto, HttpServletRequest request, HttpServletResponse response){
        Result result = new Result();
//        String token = request.getHeader("token");
        Result logResult = new Result();
        UserInfo userInfo = new UserInfo();
        try {
//            if (!redisDao.existsKey(token)) {
//                result.setCode(ResultEnum.EREOR.getCode());
//                result.setMsg("登录超时");
//                return result;
//            }
            if(explanationPointDto.getPage() == null || explanationPointDto.getPage() == 0
                    || explanationPointDto.getRows() == null || explanationPointDto.getRows() == 0){
                result.setCode(ResultEnum.DB_UPDATE_FAILURE.getCode());
                result.setMsg("缺少页码或条数");
                return result;
            }

//            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);
            result.setData(explanationPointService.getExplanationPointPage(explanationPointDto));
            result.setCode(ResultEnum.SUCCESS.getCode());
            result.setMsg("查询成功");
        } catch (Exception exception) {
            result.setCode(ResultEnum.UNKNOWNERROR.getCode());
            result.setMsg("发生错误:" + exception.getMessage());
        } finally {
            BeanUtils.copyProperties(result, logResult);
            logResult.setData(null);
            String resultCode = logResult.getCode().equals(ResultEnum.SUCCESS.getCode()) ? "SUCCESS" : "ERROR";
            logService.logAdd(explanationPointDto.toString(), "查询导览解说点分页", resultCode, logResult.toString(), userInfo, request);
        }
        return result;
    }

    @PostMapping(value = "/getExplanationPointOnLine")
    @ApiOperation(value = "查询解说点所在线路")
    @ApiImplicitParam(name = "token", paramType = "header", value = "token", dataType = "string", required = true)
    public Result getExplanationPointOnLine(@RequestBody ExplanationPoint explanationPoint, HttpServletRequest request, HttpServletResponse response){
        Result result = new Result();
//        String token = request.getHeader("token");
        Result logResult = new Result();
        UserInfo userInfo = new UserInfo();
        try {
//            if (!redisDao.existsKey(token)) {
//                result.setCode(ResultEnum.EREOR.getCode());
//                result.setMsg("登录超时");
//                return result;
//            }
            if (explanationPoint.getId() == null || "".equals(explanationPoint.getId())) {
                result.setCode(ResultEnum.DB_UPDATE_FAILURE.getCode());
                result.setMsg("缺少id");
                return result;
            }
//            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);

            result.setData(explanationPointService.getExplanationPointOnLine(explanationPoint));
            result.setCode(ResultEnum.SUCCESS.getCode());
            result.setMsg("查询成功");
        } catch (Exception exception) {
            result.setCode(ResultEnum.UNKNOWNERROR.getCode());
            result.setMsg("发生错误:" + exception.getMessage());
        } finally {
            BeanUtils.copyProperties(result, logResult);
            logResult.setData(null);
            String resultCode = logResult.getCode().equals(ResultEnum.SUCCESS.getCode()) ? "SUCCESS" : "ERROR";
            logService.logAdd(explanationPoint.toString(), "查询解说点所在线路", resultCode, logResult.toString(), userInfo, request);
        }
        return result;
    }

}
